package com.hnas.account.dao;

import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Component;

import com.hnas.account.orm.Account;
import com.hnas.common.domain.CommonStatus;
import com.hnas.common.util.CommonUtils;
import com.hnas.core.dao.BaseDaoImpl;
import com.hnas.core.util.DateUtil;
import com.hnas.core.util.StringUtil;

@Component("accountDAO")
public class AccountDAOImpl extends BaseDaoImpl<Account> implements AccountDAO {

	public AccountDAOImpl() {
		super(Account.class);
	}

	@Override
	public Account save(Account vo) {
		vo.setId(CommonUtils.getUUID());
		vo.setCreateTime(DateUtil.dateTimeToString(new Date()));
		vo.setUpdateTime(DateUtil.dateTimeToString(new Date()));
		this.persist(vo);
		return vo;
	}

	@Override
	public Account findByOpenId(String openId) {
		List<Account> list = this.findByQuery("from Account t where t.openId = ?1 and valid= ?2", openId,CommonStatus.TRUE);
		if(null!=list&&list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public Account findByPhoneNo(String phoneNo) {
		List<Account> list = this.findByQuery("from Account t where t.phoneNo = ?1 and valid= ?2", phoneNo,CommonStatus.TRUE);
		if(null!=list&&list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public void unBindOpenid(String openId) {
		String sql = "update tbl_accinfo set open_id='' where open_id='"+openId+"' and valid='"+CommonStatus.TRUE+"'";
		Query query = this.getEntityManager().createNativeQuery(sql);
		query.executeUpdate();
	}

	@Override
	public void updateOpenIdAndTime(String id, String openId, String updateTime,String appid) {
		String sql = "update tbl_accinfo set open_id='"+openId+"' ,app_id='"+appid+"', update_time='"+updateTime+"' where id='"+id+"' and valid='"+CommonStatus.TRUE+"'";
		Query query = this.getEntityManager().createNativeQuery(sql);
		query.executeUpdate();
	}

	@Override
	public void unBindAccountById(String accountId) {
		String sql = "update tbl_accinfo set open_id='',update_time='"+DateUtil.dateTimeToString(new Date())+"' where id='"+accountId+"' and valid='"+CommonStatus.TRUE+"'";
		Query query = this.getEntityManager().createNativeQuery(sql);
		query.executeUpdate();
	}

	@Override
	public void bindAcountById(String accountId,String bindOpenId) {
		String sql = "update tbl_accinfo set open_id='"+bindOpenId+"' where id='"+accountId+"' and valid='"+CommonStatus.TRUE+"'";
		Query query = this.getEntityManager().createNativeQuery(sql);
		query.executeUpdate();
	}

	@Override
	public void updateTimeBack(String accountId, String updateTime) {
		String sql = "update tbl_accinfo set update_time='"+updateTime+"' where id='"+accountId+"' and valid='"+CommonStatus.TRUE+"'";
		Query query = this.getEntityManager().createNativeQuery(sql);
		query.executeUpdate();
		
	}

	@Override
	public Account saveOrUpdate(Account vo) {
		if(StringUtil.isEmpty(vo.getId())){
			vo.setId(CommonUtils.getUUID());
			String oprTime = DateUtil.dateTimeToString(new Date());
			vo.setCreateTime(oprTime);
			vo.setUpdateTime(oprTime);
			vo.setIsDeleted(false);
			vo.setValid(CommonStatus.TRUE);
			this.persist(vo);
		}else{//修改
			vo.setUpdateTime(DateUtil.dateTimeToString(new Date()));
			this.merge(vo);
		}
		return vo;
	}

	@Override
	public Account findById(String id) {
		List<Account> list = this.findByQuery("from Account t where t.id = ?1 and valid= ?2", id,CommonStatus.TRUE);
		if(null!=list&&list.size()>0){
			return list.get(0);
		}
		return null;
	}
	
	@Override
	public Account checkedAppTokenIsExist(String accountId, String token) {
		String sql = "select * from tbl_accinfo where valid='"+CommonStatus.TRUE+"' and id='"+accountId+"' "
				+ " and app_token='"+token+"'";
		Query query = this.entityManager.createNativeQuery(sql,Account.class);
		List<Account> list = query.getResultList();
		if(null!=list&&list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public Account checkedAppTokenOverTime(String accountId, String token) {
		String sql = "select * from tbl_accinfo where valid='"+CommonStatus.TRUE+"' and id='"+accountId+"' "
				+ " and app_token='"+token+"' and  str_to_date(app_token_valid_end_time, '%Y-%m-%d %H:%i:%s')"
				+ ">str_to_date('"+DateUtil.dateTimeToString(new Date())+"', '%Y-%m-%d %H:%i:%s')";
		Query query = this.entityManager.createNativeQuery(sql,Account.class);
		List<Account> list = query.getResultList();
		if(null!=list&&list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public Account checkedWXXNKTokenIsExist(String openid, String token) {
		String sql = "select * from tbl_accinfo where valid='"+CommonStatus.TRUE+"' and open_id='"+openid+"' "
				+ " and wx_token='"+token+"'";
		Query query = this.entityManager.createNativeQuery(sql,Account.class);
		List<Account> list = query.getResultList();
		if(null!=list&&list.size()>0){
			return list.get(0);
		}
		return null;
	}
	
	@Override
	public Account checkedWXXNKTokenOverTime(String openid, String token) {
		String sql = "select * from tbl_accinfo where valid='"+CommonStatus.TRUE+"' and open_id='"+openid+"' "
				+ " and wx_token='"+token+"' and  str_to_date(wx_token_valid_end_time, '%Y-%m-%d %H:%i:%s')"
				+ ">str_to_date('"+DateUtil.dateTimeToString(new Date())+"', '%Y-%m-%d %H:%i:%s')";
		Query query = this.entityManager.createNativeQuery(sql,Account.class);
		List<Account> list = query.getResultList();
		if(null!=list&&list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public Account findByEmpId(String empId) {
		List<Account> list = this.findByQuery("from Account t where t.empId = ?1 and valid= ?2 and t.isDeleted = ?3", empId,CommonStatus.TRUE,false);
		if(null!=list&&list.size()>0){
			return list.get(0);
		}
		return null;
	}
	
	

}
